package com.quectel.util.kit;

import cn.hutool.core.codec.Base64;
import com.fasterxml.jackson.core.type.TypeReference;
import com.quectel.util.common.Response;
import com.quectel.util.http.HttpRestUtils;
import org.apache.http.HttpHeaders;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 视频流协议服务器请求器
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2023-01-06 16:46:00
 */
public class MediaConverterServerKit {

    private static final HttpRestUtils REQUESTER = HttpRestUtils.getTrustSSLInstance();

    /**
     * 获取hls264地址
     *
     * @param streamUrl     视频流地址 支持rtsp(265,264) rtmp(265,264) hls265未验证
     * @param serverBaseUrl 转换服务器地址 http://127.0.0.1:9000
     * @param username      转换服务器用户名
     * @param password      转换服务器密码
     * @return
     */
    public static String toHls(String streamUrl, String serverBaseUrl, String username, String password) {
        Map<String, String> headers = new HashMap<>();
        headers.put(HttpHeaders.AUTHORIZATION, "Basic " + Base64.encode(username.concat(":").concat(password), StandardCharsets.UTF_8));

        Map<String, String> params = new HashMap<>();
        params.put("streamUrl", streamUrl);
        Response<String> response = REQUESTER.post(
                serverBaseUrl + "/toHls",
                params,
                headers,
                new TypeReference<Response<String>>() {
                }
        ).getValue();
        if (response.isSuccess()) {
            return response.getData();
        }
        return null;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(toHls("rtsp://admin:dh123456@10.88.107.16:554/cam/realmonitor?channel=1&subtype=1", "http://10.88.107.201:9999/", "test", "test"));
        }
    }

}
